//
//  WXTextView.swift
//  weibo
//
//  Created by 王旭 on 16/4/26.
//  Copyright © 2016年 王旭. All rights reserved.

//写微博的自定义View

import UIKit

class WXTextView: UITextView {
    
    var placeholder : String?{
        didSet{
            placeLabel.text = placeholder
        }
    }
    
    //使Label字体的大小和外界设置保持一致
    override var font : UIFont?{
        didSet{
            placeLabel.font = self.font
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        addSubview(placeLabel)
        
        placeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(self).offset(-10)
        }
        
        //通知监听textView的输入
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textValueChange", name: UITextViewTextDidChangeNotification, object: nil)
    }
    
//文本改变方法
    @objc private func textValueChange() {
        placeLabel.hidden = self.hasText()
    }
    
//懒加载占位Label
    private lazy var placeLabel : UILabel = {
        let placeLabel = UILabel(textColor: UIColor.blueColor(), fontSize: 14)
        placeLabel.text = "为为何怪我列无诶个为"
        placeLabel.numberOfLines = 0
        return placeLabel
    }()
    
//移除通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

}
